package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.MaterielFromAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.from.FromSearchBean;
import com.glory.hikvisionmobile.bean.from.MaterielRecordBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.customView.HikLabelEditText;
import com.glory.hikvisionmobile.customView.HikLabelTextView;
import com.glory.hikvisionmobile.customView.HikTabView;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.datepicker.CustomDatePicker;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/3/24 11:49
 * @DESC *****************************
 */
public class From_MaterielSearchActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lbStartTime)
    HikLabelTextView lbStartTime;
    @BindView(R.id.lbEndTime)
    HikLabelTextView lbEndTime;
    @BindView(R.id.lbMaterielName)
    HikLabelEditText lbMaterielName;
    @BindView(R.id.tabView)
    HikTabView tabView;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;

    private CustomDatePicker mStartDatePicker;
    private CustomDatePicker mEndDatePicker;

    private MaterielFromAdapter materielFromAdapter;

    private boolean newBuild;
    private boolean search;
    private String startTime;
    private String endTime;

    private String[] tabs = new String[]{"日期", "作业类型", "物料名称", "数量", "剩余库存"};

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_from_materiel_search;
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("物料领退记录表");
        freeUI_titleView.setRightListener("新建", v -> {
            if (newBuild) {
                goRecord(null);
            } else {
                FreeApi_ToastUtils.showFreeToast("暂无权限",this, false, Toast.LENGTH_SHORT);
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @Override
    protected void initView() {

        initTabs();

        materielFromAdapter = new MaterielFromAdapter(null);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(materielFromAdapter);
        materielFromAdapter.setOnItemClickListener((adapter, view, position) -> {
            goRecord((MaterielRecordBean) adapter.getData().get(position));
        });
        initDatePicker();
    }

    private void initTabs() {
        List<HikTabView.Tab> tabs = new ArrayList<>();
        for (int i = 0; i < this.tabs.length; i++) {
            tabs.add(new HikTabView.Tab(this.tabs[i], 1, Gravity.CENTER));
        }
        tabView.setTabs(tabs);
    }

    private void initDatePicker() {
        long beginTimestamp = FreeApi_DateUtils.str2Long("1992-03-20", false);
        long endTimestamp = FreeApi_DateUtils.str2Long("2222-02-22", false);

        mStartDatePicker = new CustomDatePicker(this, timestamp -> {
            startTime = FreeApi_DateUtils.long2Str(timestamp, false);
            lbStartTime.getLabelContext().setText(startTime);
        }, beginTimestamp, endTimestamp);
        mStartDatePicker.setCancelable(true);
        mStartDatePicker.setCanShowPreciseTime(false);
        mStartDatePicker.setScrollLoop(false);
        mStartDatePicker.setCanShowAnim(false);

        mEndDatePicker = new CustomDatePicker(this, timestamp -> {
            endTime = FreeApi_DateUtils.long2Str(timestamp, false);
            lbEndTime.getLabelContext().setText(endTime);
        }, beginTimestamp, endTimestamp);
        mEndDatePicker.setCancelable(true);
        mEndDatePicker.setCanShowPreciseTime(false);
        mEndDatePicker.setScrollLoop(false);
        mEndDatePicker.setCanShowAnim(false);
    }

    @OnClick({R.id.lbStartTime, R.id.lbEndTime, R.id.tvSubmit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lbStartTime:
                mStartDatePicker.show(System.currentTimeMillis());
                break;
            case R.id.lbEndTime:
                mEndDatePicker.show(System.currentTimeMillis());
                break;
            case R.id.tvSubmit:
                if (search) {
                    search();
                } else {
                    FreeApi_ToastUtils.showFreeToast("暂无权限",this, false, Toast.LENGTH_SHORT);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        newBuild = bundle.getBoolean("NewBuild", false);
        search = bundle.getBoolean("Search", false);
    }

    private void search() {
        if (!FreeApi_StringUtils.isEmpty(startTime) && !FreeApi_StringUtils.isEmpty(endTime)) {
            int i = FreeApi_DateUtils.compare_date(endTime, startTime, "yyyy-MM-dd");
            if (i != 1) {
                showToast("结束日期不能大于开始日期", false);
                return;
            }
        } else {
            showToast("开始时间和结束时间不能为空", false);
            return;
        }

        JsonObject map = new JsonObject();
        map.addProperty("actionType", "query");
        map.addProperty("formType", "WLLT");
        map.addProperty("fromTime", startTime);
        map.addProperty("toTime", endTime);
        map.addProperty("materialName",lbMaterielName.getLabelContext().getText().toString().trim());
        NetUtils.requestDara("HIK.PdaForm", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<FromSearchBean>>
                (new TypeToken<BaseResponseBean<FromSearchBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<FromSearchBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    materielFromAdapter.setNewData(response.body().getBody().getxPdaForms().getxPdaFormMatLingTuis());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<FromSearchBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaForm");
            }
        });
    }

    private void goRecord(MaterielRecordBean materielRecordBean){
        if (materielRecordBean != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("MaterielRecordBean", materielRecordBean);
            FreeApi_Utils.skipWithExtraIntent("header", bundle, this, From_MaterielRecordActivity.class);
        } else {
            FreeApi_Utils.skipWithExtraIntent("header", this, From_MaterielRecordActivity.class);
        }
    }
}
